cmake_minimum_required(VERSION 3.5)

project(iap-cgfd-demo Fortran)

if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
  add_definitions(-DFC_IS_GNU)
  list(APPEND fortran_flags
    -fdefault-real-8
    -ffree-line-length-none
    -fno-range-check
    -fallow-argument-mismatch
    -fallow-invalid-boz
  )
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    list(APPEND fortran_flags -O0 -fcheck=all -g -fbacktrace)
  else ()
    list(APPEND fortran_flags -Ofast -flto)
  endif ()
  # FIXME: OpenMP causes sigmentation faults.
  # list(APPEND fortran_flags -fopenmp)
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel" OR CMAKE_Fortran_COMPILER_ID STREQUAL "IntelLLVM")
  add_definitions(-DFC_IS_INTEL)
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    list(APPEND fortran_flags
      -no-wrap-margin
      -O0
      -check bounds
      -g
      -traceback
      -fp-model precise
    )
  else ()
    list(APPEND fortran_flags
      -no-wrap-margin
      -O3
      -align array64byte
      # -fp-model precise
    )
  endif ()
endif ()
string(REPLACE ";" " " CMAKE_Fortran_FLAGS "${fortran_flags}")


if (DEFINED ENV{NETCDF} AND (NOT DEFINED ENV{NETCDF_ROOT}))
  set(ENV{NETCDF_ROOT} $ENV{NETCDF})
endif ()
if (DEFINED ENV{NETCDF_ROOT})
  include_directories("$ENV{NETCDF_ROOT}/include")
  link_directories("$ENV{NETCDF_ROOT}/lib")
else ()
  find_package(PkgConfig)
  if (${PKG_CONFIG_FOUND})
    pkg_search_module(NETCDF_FORTRAN REQUIRED netcdf-fortran)
    if (${NETCDF_FORTRAN_FOUND})
      include_directories (${NETCDF_FORTRAN_INCLUDEDIR})
      link_directories (${NETCDF_FORTRAN_LIBDIR})
    else ()
      message(FATAL_ERROR "Pkg-config could not find netcdf library!")
    endif ()
  else ()
    message(FATAL_ERROR "Unable to find pkg-config library!")
  endif ()
endif ()

add_subdirectory(advection/ftcs)
add_subdirectory(advection/upwind)
add_subdirectory(advection/beam_warming)
add_subdirectory(advection/lax_wendroff)
add_subdirectory(advection/fromm)
add_subdirectory(advection/leap_frog)
add_subdirectory(advection/crank_nicolson)
add_subdirectory(advection/tspas)
add_subdirectory(advection/mpdata)
add_subdirectory(advection/weno)
add_subdirectory(advection/tvd)
add_subdirectory(advection/ffsl)
add_subdirectory(advection/semi_lagrangian)
add_subdirectory(advection/bimocq)
add_subdirectory(linear_swm)
